package edu.dqsy.mzq_translate;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import edu.dqsy.mzq_translate.helper.ContactOpenHelper;

import static edu.dqsy.mzq_translate.denglu.user;

public class CreateDancitable extends AppCompatActivity {
    public String username;
    final ContactOpenHelper helper = new ContactOpenHelper(CreateDancitable.this,"userinfo.db",null,1);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_dancitable);
        username = user;
    }


    public void back(View v){
        Intent ii = new Intent(CreateDancitable.this, Danciben.class);
        startActivity(ii);
    }

    public void create(View v){
        helper.getWritableDatabase();
        SQLiteDatabase db = helper.getWritableDatabase();
        AlertDialog.Builder builder  = new AlertDialog.Builder(CreateDancitable.this);
        TextView n = findViewById(R.id.danciname);
        TextView in = findViewById(R.id.danciinfo);
        String name = n.getText().toString();
        String info = in.getText().toString();
        Cursor c = db.rawQuery("SELECT * FROM "+username+" where name=?",new String[]{username+"_"+name});
        if (c.getCount() != 0){
            builder.setTitle("添加失败" ) ;
            builder.setPositiveButton("是" ,  null );
            builder.setMessage("已存在此单词表" ) ;
            builder.show();
        }else {
            db.execSQL("Insert into "+username+" (name,info) values(?,?)",new String[]{name,info});
            db.execSQL( "create table "+username+"_"+name+"(_id integer primary key autoincrement,zh varchar(20),en varchar(20))");
            db.close();
            builder.setTitle("添加成功" ) ;
            builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent ii = new Intent(CreateDancitable.this, Danciben.class);
                    startActivity(ii);
                }
            });
            builder.show();
        }

    }
}
